package com.example.mydemo103;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.mydemo103.DB.UserService;
import com.example.mydemo103.bean.User;
import com.example.mydemo103.util.Config;

public class LgoinActivity extends AppCompatActivity {
    private Button login_btn;
    private TextView regiest;
    private EditText user,pwd;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lgoin);
    login_btn=findViewById(R.id.login_button);
    user=findViewById(R.id.login_PersonName);
    pwd=findViewById(R.id.login_passward);
    regiest=findViewById(R.id.login_regiest_button);

//    登录与注册按钮左边图片
    Drawable drawablebyuser=getResources().getDrawable(R.drawable.user);
    drawablebyuser.setBounds(0,0,45,55);//第一0是距左边距离，第二0是距上边距离，30、35分别是长宽
    user.setCompoundDrawables(drawablebyuser,null,null,null);
    Drawable drawablebypwd=getResources().getDrawable(R.drawable.pwd);
    drawablebypwd.setBounds(0,0,45,55);//第一0是距左边距离，第二0是距上边距离，30、35分别是长宽
    pwd.setCompoundDrawables(drawablebypwd,null,null,null);

//    去除标题栏
    if (getSupportActionBar() != null){
            getSupportActionBar().hide();
    }
    login_btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String name=user.getText().toString();
            String password=pwd.getText().toString();
            UserService userService=new UserService(LgoinActivity.this);
            if (name.equals("")||password.equals("")){
                Toast.makeText(LgoinActivity.this,"账号或密码不能为空",Toast.LENGTH_LONG).show();
            }else {
                User user= (User) userService.checkLogin(name,password);
                if (user!=null){
                    Config.auser=user;
                    Toast.makeText(LgoinActivity.this,"欢迎您："+user.getUsername(),Toast.LENGTH_LONG).show();
                    Intent intent=new Intent(LgoinActivity.this,MainActivity.class);
                    intent.putExtra("username",user.getUsername());
                    startActivity(intent);
                }else {
                    Toast.makeText(LgoinActivity.this,"账号或密码错误",Toast.LENGTH_LONG).show();
                }
            }
        }
    });
        regiest.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent=new Intent(LgoinActivity.this,RegietActivity.class);
            startActivity(intent);

        }
    });
    }

    @Override
    protected void onPause() {
        super.onPause();
        finish();
    }
}